﻿using System;
using System.Web;
using System.Collections;
using LRJ.Profile;
using LRJ.Text;
using CookComputing.XmlRpc;

namespace LRJ.Snipplr
{


    public class SnippetController
    {
        private SnippetListModel m_list;
        private SnippetModel m_snippet;

        private bool _public = true;

        public string Id
        {
            get { return m_list.id; }
        }
        public bool Favorite
        {
            get { return m_list.favorite; }
        }
        public DateTime Update
        {
            get { return m_list.updated.datetime; }
        }
        public string UserId
        {
            get { return m_snippet.user_id; }
        }
        public string Username
        {
            get { return m_snippet.username; }
        }
        public string Title
        {
            get { return m_snippet.title; }
            set { m_snippet.title = value; }
        }
        public string Language
        {
            get { return m_snippet.language; }
            set { m_snippet.language = value; }
        }
        public string Code
        {
            get { return m_snippet.source; }
            set { m_snippet.source = value; }
        }
        public string Comment
        {
            get { return m_snippet.comment; }
            set { m_snippet.comment = value; }
        }
        public string[] Tags
        {
            get { return m_snippet.tags.Trim().Split(' '); }
            set { m_snippet.tags = string.Join(" ", value); }
        }
        public DateTime Created
        {
            get { return DateTime.Parse(m_snippet.created); }
        }
        public string SnipplrUrl
        {
            get { return m_snippet.snipplr_url; }
        }
        public bool Public
        {
            get { return _public; }
        }
        public SnippetController(SnippetListModel entry, IApiProxy proxy)
        {
            m_list = entry;
            try
            {
                m_snippet = proxy.GetSnippet(entry.id);
            }
            catch (CookComputing.XmlRpc.XmlRpcFaultException)
            {
                _public = false;
            }
        }
    }
    public class SnippetCollection : CollectionBase
    {
        public IApiProxy Proxy;
        public SnippetController this[int index]
        {
            get { return ((SnippetController)this.List[index]); }
            set { this.List[index] = value; }
        }

        public SnippetCollection(SnippetListModel[] entries)
        {
            Proxy = SnipplrProxy.Create();
            for (int i = 0; i <= entries.Length - 1; i++)
            {
                this.Add(new SnippetController(entries[i], Proxy));
            }
        }

        public int Add(SnippetController snippet)
        {
            return (this.List.Add(snippet));
        }

        public int IndexOf(SnippetController snippet)
        {
            return (this.List.IndexOf(snippet));
        }

        public void Insert(int index, SnippetController snippet)
        {
            this.List.Insert(index, snippet);
        }

        public void Remove(SnippetController snippet)
        {
            this.List.Remove(snippet);
        }

        public new void RemoveAt(int index)
        {
            this.List.RemoveAt(index);
        }

        public bool Contains(SnippetController snippet)
        {
            return this.List.Contains(snippet);
        }
    }

}
